package com.oa.overtime.dto;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
import java.util.List;

import javax.persistence.criteria.CriteriaBuilder;
import javax.persistence.criteria.CriteriaQuery;
import javax.persistence.criteria.Predicate;
import javax.persistence.criteria.Root;

import org.springframework.data.jpa.domain.Specification;

import com.fasterxml.jackson.annotation.JsonFormat;
import com.oa.overtime.entity.Overtime;

public class OvertimeDTO {
	/**
	 * 表单信息
	 */
	private Long id;
	private String name;
	private Date createDate;
	private Date applicationTime;
	private Date beginDate;
	private Date endDate;
	private String department;
	private String overtimeDate;
	private String beginTime;
	private String endTime;
	private Float totalTime;
	private String reason;
	private String rejectReason;
	private Date realBeginDate;
	private Date realEndDate;
	private Float realTotalTime;
	private String realOvertimeDate;
	private String realBeginTime;
	private String realEndTime;

	/**
	 * 工作流信息
	 */
	private Long userId;
	private String processInstanceId;
	private String taskId;
	private String state;
	private String assignee;
	private String taskDefinitionKey;
	private String taskName;

	public static Specification<Overtime> getWhereClause(OvertimeDTO overtimeDTO) {
		return new Specification<Overtime>() {
			@Override
			public Predicate toPredicate(Root<Overtime> root, CriteriaQuery<?> cq, CriteriaBuilder cb) {
				List<Predicate> predicates = new ArrayList<>();
				if (overtimeDTO.getName() != null && overtimeDTO.getName().trim().length() > 0) {
					Predicate p1 = cb.like(root.get("name").as(String.class), "%" + overtimeDTO.getName() + "%");
					predicates.add(p1);
				}
				if (overtimeDTO.getCreateDate() != null) {
					Calendar calendar = Calendar.getInstance();
					calendar.setTime(overtimeDTO.getCreateDate());
					calendar.add(Calendar.DATE, 1);
					Date tomorrow = new Date(new SimpleDateFormat("yyyy/MM/dd HH:mm:ss").format(calendar.getTime()));

					Predicate p2 = cb.between(root.get("createDate"), overtimeDTO.getCreateDate(), tomorrow);
					predicates.add(p2);
				}
				if (overtimeDTO.getState() != null && overtimeDTO.getState().trim().length() > 0) {
					Predicate p3 = cb.like(root.get("state").as(String.class), overtimeDTO.getState());
					predicates.add(p3);
				}
				if (overtimeDTO.getApplicationTime() != null) {
					Calendar calendar = Calendar.getInstance();
					calendar.setTime(overtimeDTO.getApplicationTime());
					calendar.add(Calendar.DATE, 1);
					Date tomorrow = new Date(new SimpleDateFormat("yyyy/MM/dd HH:mm:ss").format(calendar.getTime()));

					Predicate p4 = cb.between(root.get("applicationTime"), overtimeDTO.getApplicationTime(), tomorrow);
					predicates.add(p4);
				}
				if (overtimeDTO.getDepartment() != null && overtimeDTO.getDepartment().trim().length() > 0) {
					Predicate p5 = cb.like(root.get("department").as(String.class), overtimeDTO.getDepartment());
					predicates.add(p5);
				}
				Predicate[] aPredicates = new Predicate[predicates.size()];
				return cb.and(predicates.toArray(aPredicates));
			}
		};
	}

	public static Specification<Overtime> getOvertimeWhereClause(OvertimeDTO overtimeDTO) {
		return new Specification<Overtime>() {

			@Override
			public Predicate toPredicate(Root<Overtime> root, CriteriaQuery<?> cq, CriteriaBuilder cb) {
				List<Predicate> predicates = new ArrayList<>();
				if (overtimeDTO.getName() != null && overtimeDTO.getName().trim().length() > 0) {
					Predicate p1 = cb.like(root.get("name").as(String.class), overtimeDTO.getName());
					predicates.add(p1);
				}
				if (overtimeDTO.getCreateDate() != null) {
					Calendar calendar = Calendar.getInstance();
					calendar.setTime(overtimeDTO.getCreateDate());
					calendar.add(Calendar.DATE, 1);
					Date tomorrow = new Date(new SimpleDateFormat("yyyy/MM/dd HH:mm:ss").format(calendar.getTime()));

					Predicate p2 = cb.between(root.get("createDate"), overtimeDTO.getCreateDate(), tomorrow);
					predicates.add(p2);
				}
				Predicate[] aPredicates = new Predicate[predicates.size()];
				return cb.and(predicates.toArray(aPredicates));
			}
		};
	}


	public Long getId() {
		return id;
	}

	public String getName() {
		return name;
	}

	@JsonFormat(pattern = "yyyy/MM/dd HH:mm:ss", timezone = "GMT+8")
	public Date getApplicationTime() {
		return applicationTime;
	}

	public String getDepartment() {
		return department;
	}

	public String getBeginTime() {
		return beginTime;
	}

	public String getEndTime() {
		return endTime;
	}

	public Float getTotalTime() {
		return totalTime;
	}

	public String getReason() {
		return reason;
	}

	public String getOvertimeDate() {
		return overtimeDate;
	}

	@JsonFormat(pattern = "yyyy/MM/dd HH:mm:ss", timezone = "GMT+8")
	public Date getBeginDate() {
		return beginDate;
	}

	@JsonFormat(pattern = "yyyy/MM/dd HH:mm:ss", timezone = "GMT+8")
	public Date getEndDate() {
		return endDate;
	}

	public String getTaskId() {
		return taskId;
	}

	public String getState() {
		return state;
	}

	public Long getUserId() {
		return userId;
	}

	public String getProcessInstanceId() {
		return processInstanceId;
	}

	@JsonFormat(pattern = "yyyy/MM/dd HH:mm:ss", timezone = "GMT+8")
	public Date getCreateDate() {
		return createDate;
	}

	public String getAssignee() {
		return assignee;
	}

	public String getTaskDefinitionKey() {
		return taskDefinitionKey;
	}

	public String getTaskName() {
		return taskName;
	}

	public String getRejectReason() {
		return rejectReason;
	}

	@JsonFormat(pattern = "yyyy/MM/dd HH:mm:ss", timezone = "GMT+8")
	public Date getRealBeginDate() {
		return realBeginDate;
	}

	@JsonFormat(pattern = "yyyy/MM/dd HH:mm:ss", timezone = "GMT+8")
	public Date getRealEndDate() {
		return realEndDate;
	}

	public Float getRealTotalTime() {
		return realTotalTime;
	}

	public String getRealOvertimeDate() {
		return realOvertimeDate;
	}

	public String getRealBeginTime() {
		return realBeginTime;
	}

	public String getRealEndTime() {
		return realEndTime;
	}

	public void setRealOvertimeDate(String realOvertimeDate) {
		this.realOvertimeDate = realOvertimeDate;
	}

	public void setRealBeginTime(String realBeginTime) {
		this.realBeginTime = realBeginTime;
	}

	public void setRealEndTime(String realEndTime) {
		this.realEndTime = realEndTime;
	}

	public void setRejectReason(String rejectReason) {
		this.rejectReason = rejectReason;
	}

	public void setRealBeginDate(Date realBeginDate) {
		this.realBeginDate = realBeginDate;
	}

	public void setRealEndDate(Date realEndDate) {
		this.realEndDate = realEndDate;
	}

	public void setRealTotalTime(Float realTotalTime) {
		this.realTotalTime = realTotalTime;
	}

	public void setTaskDefinitionKey(String taskDefinitionKey) {
		this.taskDefinitionKey = taskDefinitionKey;
	}

	public void setTaskName(String taskName) {
		this.taskName = taskName;
	}

	public void setAssignee(String assignee) {
		this.assignee = assignee;
	}

	public void setCreateDate(Date createDate) {
		this.createDate = createDate;
	}

	public void setTaskId(String taskId) {
		this.taskId = taskId;
	}

	public void setState(String state) {
		this.state = state;
	}

	public void setUserId(Long userId) {
		this.userId = userId;
	}

	public void setProcessInstanceId(String processInstanceId) {
		this.processInstanceId = processInstanceId;
	}



	public void setBeginDate(Date beginDate) {
		this.beginDate = beginDate;
	}

	public void setEndDate(Date endDate) {
		this.endDate = endDate;
	}

	public void setOvertimeDate(String overtimeDate) {
		this.overtimeDate = overtimeDate;
	}

	public void setId(Long id) {
		this.id = id;
	}

	public void setName(String name) {
		this.name = name;
	}

	public void setApplicationTime(Date applicationTime) {
		this.applicationTime = applicationTime;
	}

	public void setDepartment(String department) {
		this.department = department;
	}

	public void setBeginTime(String beginTime) {
		this.beginTime = beginTime;
	}

	public void setEndTime(String endTime) {
		this.endTime = endTime;
	}

	public void setTotalTime(Float totalTime) {
		this.totalTime = totalTime;
	}

	public void setReason(String reason) {
		this.reason = reason;
	}

}
